<?php
/*---------------------------------------------------------------------
 * @function 应用的公共的常用的全局函数
 * @author: yangjian
 * @since 2016-11-07
 *-----------------------------------------------------------------------*/
//获取语言包
if ( !function_exists('LANG') ) {
    function LANG($key) {
        if ( !$key ) return false;
        $langs = \herosphp\core\Loader::config('language');
        return $langs[$key];
    }
}

//获取短信模板
if ( !function_exists('MT') ) {
    function MT($key) {
        if ( !$key ) return false;
        $message = \herosphp\core\Loader::config('message');
        return isset($message[$key])?$message[$key]:false;
    }
}

//设置appID
if ( !function_exists('setAPPID') ) {
    function setAPPID($appid) {
        define('__APPID__', $appid);
    }
}

//获取appID {area 1,2,3}
if ( !function_exists('getAPPID') ) {
    function getAPPID() {
        if ( defined('__APPID__') ) {
            return __APPID__;
        } else {
            return false;
        }
    }
}
if( !function_exists('getAreaName') ){
    function getAreaName($areaid){
        if( !$areaid ){
            return "";
        }
        $areaService = \herosphp\bean\Beans::get('system.area.service');
        $item = $areaService -> getItem(array('id'=>$areaid),'name',null);
        return $item['name'];
    }
}

/**
 * 判断一个银行卡号是否是虚拟帐号
 * @param $cardnO
 * @return bool
 */
function isVirtualAccount($cardnO) {
    return strpos($cardnO, "955") === 0;
}

/**
 * excel导出
 */
if (!function_exists('phpexcel')) {
    function phpexcel($name, $title, $data, $type = 1, $second_title = null, $second_data = null)
    {
        $objPHPExcel = new PHPExcel();
        /*以下是一些设置 ，什么作者  标题啊之类的*/
        $objPHPExcel->getProperties()->setCreator('Fiidee.Monda.Cn')
            ->setLastModifiedBy("Fiidee.Monda.Cn")
            ->setTitle("Fiidee.Monda.Cn")
            ->setSubject("Fiidee.Monda.Cn")
            ->setDescription("Fiidee.Monda.Cn")
            ->setKeywords("Fiidee.Monda.Cn")
            ->setCategory("Fiidee.Monda.Cn");
        //以下就是对处理Excel里的数据， 横着取数据，主要是这一步，其他基本都不要改
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setTitle($name);
        $objPHPExcel->getActiveSheet()->getColumnDimension()->setAutoSize(true);
        //第一行默认为表头
        //在设置单元表格值的时候，前面加一个空格，可以强转为字符串，避免出现科学计数法
        $row = 1;
        if ($title && is_array($title)) {
            $col = 0;
            foreach ($title as $v) {
                $objPHPExcel->getActiveSheet()->
                setCellValueExplicit(chr($col+65).$row,$v,PHPExcel_Cell_DataType::TYPE_STRING);
                //setCellValueByColumnAndRow($col, $row, " ".$v);
                $col++;
            }
            $row++;
        }
        //表格内容
        foreach ($data as $k => $v) {
            $col = 0;
            foreach ($v as $val) {
                if ($col + 1 > count($title)) continue;
                $objPHPExcel->getActiveSheet()
                    ->setCellValueExplicit(chr($col+65).$row,$val,PHPExcel_Cell_DataType::TYPE_STRING);
                //->setCellValueByColumnAndRow($col, $row, " ".$val);
                $col++;
            }
            $row++;
        }
        if ($type == 2) {//多表 应用于财务账单列表
            $num = 1;
            foreach ($second_data as $k => $v) {
                if ($v)
                    $objPHPExcel->createSheet();
                $objPHPExcel->setActiveSheetIndex($num);
                $objPHPExcel->getActiveSheet()->setTitle($data[$k]['name'] . '清单');
                //第一行默认为表头
                $row = 1;
                $col = 0;
                if ($second_title && is_array($second_title)) {
                    foreach ($second_title as $val) {
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, " ".$val);
                        $col++;
                    }
                    $row++;
                }
                //表格内容
                foreach ($v as $val) {
                    $col = 0;
                    foreach ($val as $value) {
                        if ($col + 1 > count($second_title)) continue;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, " ".$value);
                        $col++;
                    }
                    $row++;
                }
                $num++;
            }
        }

        $name = $name . date("YmdHis");
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $name . '.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }

}
